﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Manageweb_TiXian_TiXian : System.Web.UI.Page
{
    SQLExpressDAL.DealRule.WebSiteConfigDataTable configTable = null;
    private Decimal _accountMoney;
    public Decimal AccountMoney
    {
        get
        {
            if (ViewState["accountMoney"] == null)
            {
                ViewState["accountMoney"] = 0.00m;
            }
            return Convert.ToDecimal(ViewState["accountMoney"]);
        }
        set
        {
            ViewState["accountMoney"] = value;
            _accountMoney = value;
        }
    }
    public string UserName;

    public string StrKey;

    protected override void Render(HtmlTextWriter writer)
    {
        System.IO.StringWriter sw = new System.IO.StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        base.Render(htw);

        if (!hidMiYao.Value.Equals("1"))
        {
            writer.Write(sw.ToString().Replace("<body", "<body onload=javascript:openLayer('test','test_con');"));
        }
        else
        {
            writer.Write(sw);
        }
    }


    //获取密钥Cdefine5
    public string CheckData()
    {
        SqlParameter[] paras ={ 
                new SqlParameter("@userid",SqlDbType.VarChar,30)
            };
        paras[0].Value = Session["userid"];

        StrKey = SQLExpressDAL.SqlHelper.FillDataSet(CommandType.StoredProcedure, "UP_GetUserKey", paras).Tables[0].Rows[0][1].ToString();
        return StrKey;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
         StrKey = CheckData();
         if (hidMiYao.Value.Equals("1"))
         {
             this.divTiXian.Visible = true;
             //InitUserInfo();
             ////InitAccountMoney();
             //GetUserAccountInfo();

             //txtFei.Attributes.Add("readonly", "true");
             //txtMoney.Attributes.Add("readonly", "true");
         }

         if (!Page.IsPostBack)
         {
             InitUserInfo();
             //InitAccountMoney();
             GetUserAccountInfo();

             txtFei.Attributes.Add("readonly", "true");
             txtMoney.Attributes.Add("readonly", "true");
         }
    }

    /// <summary>
    /// 初始化账户的金额信息
    /// </summary>
    protected void GetUserAccountAB1Info()
    {
        SQLExpressDAL.TransferRecordsTableAdapters.UsersAccountTableAdapter adp = new SQLExpressDAL.TransferRecordsTableAdapters.UsersAccountTableAdapter();
        SQLExpressDAL.TransferRecords.UsersAccountDataTable tab = adp.GetSingleAccount(Session["UserID"].ToString());

        SQLExpressDAL.TransferRecords.UsersAccountRow dr = (SQLExpressDAL.TransferRecords.UsersAccountRow)tab.Rows[0];

        AccountMoney = dr.AB1;

    }

    protected void InitUserInfo()
    {
        SQLExpressDAL.Users.UsersDataTable userInfo = Session["UserInfo"] as SQLExpressDAL.Users.UsersDataTable;
        this.txtAccount.Text = userInfo.Rows[0]["UserID"].ToString();
        UserName = userInfo.Rows[0]["UserName"].ToString();
        txtUserBankName.Text = userInfo.Rows[0]["Cdefine6"].ToString();
        SQLExpressDAL.DealRuleTableAdapters.WebSiteConfigTableAdapter configAdp = new SQLExpressDAL.DealRuleTableAdapters.WebSiteConfigTableAdapter();
        configTable = configAdp.GetData();
        DataRow[] drs = configTable.Select(" configname='LV'");
        HiddenField1.Value = drs[0]["configvalue"].ToString();
    }


    /// <summary>
    /// 初始化账户的金额信息(左侧列表)
    /// </summary>
    protected void GetUserAccountInfo()
    {
        SQLExpressDAL.TransferRecordsTableAdapters.UsersAccountTableAdapter adp = new SQLExpressDAL.TransferRecordsTableAdapters.UsersAccountTableAdapter();
        SQLExpressDAL.TransferRecords.UsersAccountDataTable tab = adp.GetSingleAccount(Session["UserID"].ToString());

        SQLExpressDAL.TransferRecords.UsersAccountRow dr = (SQLExpressDAL.TransferRecords.UsersAccountRow)tab.Rows[0];
        Label lblAB1 = Master.FindControl("lblAB1") as Label;

        lblAB1.Text = dr.AB1.ToString("f2");
        AccountMoney = Convert.ToDecimal(dr.AB1);

    }

    protected void btnShengQing_Click(object sender, EventArgs e)
    {
        try
        {

            String userCode = this.txtAccount.Text.Trim();
            String bankname = DDLBank.SelectedValue;
            String bankarea = ddl_area.SelectedValue;
            String Bankfen = this.txtBankfen.Text.Trim();
            String UserBackCode = this.txtBackCode.Text.Trim();
            String UserBankName = this.txtUserBankName.Text.Trim();
            Decimal MaoMoney = Convert.ToDecimal(this.txtMaoMoney.Text.Trim());
           
            Decimal Fei = Convert.ToDecimal(this.txtFei.Text.Trim());
            Decimal TXMoney = Convert.ToDecimal(this.txtMoney.Text.Trim());
            String Cmemo = txtMemo.Text.Trim();

            if (!CheckTXMoney())
            {
                return;
            }


            SQLExpressDAL.StockTableAdapters.TiXianTableAdapter adp = new SQLExpressDAL.StockTableAdapters.TiXianTableAdapter();
            int flag = adp.InsertTXAccess(userCode, bankname, bankarea, Bankfen, UserBackCode, UserBankName, MaoMoney, Fei, TXMoney, Cmemo);

            if (flag > 0)
            {
                this.msg.Text = "申请成功,请等待管理员审核.";
            }
            else
            {
                this.msg.Text = "操作失败!";
            }
        }
        catch (Exception ex)
        {
            this.msg.Text = "操作失败!";
        }
        finally
        {
            GetUserAccountAB1Info();
            GetUserAccountInfo();
            this.txtMoney.Text = "";
            BindTXData();
        }
    }


    //private void InitAccountMoney()
    //{
    //    SQLExpressDAL.StockTableAdapters.RecordTableAdapter recordAdp = new SQLExpressDAL.StockTableAdapters.RecordTableAdapter();
    //    AccountMoney = Convert.ToDecimal(recordAdp.QueryAccountMoney(Session["UserID"].ToString()));
    //}


        /// <summary>
    /// 检验是否超过账户金额
    /// </summary>
    /// <returns></returns>
    protected bool CheckTXMoney()
    {
        bool flag = true;

        decimal currMoney = AccountMoney;
        decimal inputMoney = Convert.ToDecimal(this.txtMaoMoney.Text.Trim());

        if (inputMoney > currMoney)
        {
            flag = false;
        }

        if (!flag)
        {
            this.msg.Text = "账户金额不足";
        }
        return flag;
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindTXData();
    }

    protected void BindTXData()
    {
        //SQLExpressDAL.StockTableAdapters.TiXianTableAdapter adp = new SQLExpressDAL.StockTableAdapters.TiXianTableAdapter();
        //this.GV_TXList.DataSource = adp.GetAllTXBill();
        //this.GV_TXList.DataBind();
        SQLExpressDAL.DealRecord obj = new SQLExpressDAL.DealRecord();
        this.GV_TXList.DataSource = obj.GetTiXianRecord(Session["UserID"].ToString(), this.txtBeginDate.Text.Trim(), this.txtEndDate.Text.Trim());
        this.GV_TXList.DataBind();
    }
    protected void GV_TXList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GV_TXList.PageIndex = e.NewPageIndex;
        BindTXData();
    }

    protected void lbtnApprove_Command(object sender, CommandEventArgs e)
    {
        try
        {

            SQLExpressDAL.StockTableAdapters.TiXianTableAdapter adp = new SQLExpressDAL.StockTableAdapters.TiXianTableAdapter();
            int flag = adp.UpdateTXState(Session["UserID"].ToString(), DateTime.Now, 3, Convert.ToInt32(e.CommandArgument));

            if (flag > 0)
            {
                this.msg.Text = "取消成功!";
            }
            else
            {
                this.msg.Text = "取消失败!";
            }
        }
        catch (Exception ex)
        {
            this.msg.Text = ex.Message;
        }
        finally
        {
            
            BindTXData();
            
        }
    }
}
